package lxq.away3d.controller;

import away3d.cameras.Camera3D;
import away3d.containers.ObjectContainer3D;
import away3d.containers.View3D;
import away3d.controllers.HoverController;
import away3d.controllers.LookAtController;
import away3d.core.base.Object3D;
import flash.display.InteractiveObject;
import flash.display.Stage;
import flash.events.MouseEvent;
/**
 * ...
 * @author lxq
 */
class CusController {
private var stage:InteractiveObject;
private var camera:Camera3D;

private var active:Bool;

private var lastPanAngle:Float;
private var lastTiltAngle:Float;
private var lastMouseX:Float;
private var lastMouseY:Float;
public var lastDistance:Float;

public var hover:HoverController;

public var lookatCon:LookAtController;
public function new(stage:InteractiveObject,camera:Camera3D,view:View3D,defaultDistance:Float=1000) {
	this.stage = stage;
	this.camera = camera;
	this.lastDistance = defaultDistance;
	hover = new HoverController(camera, null, 0, 45, defaultDistance);
	//lookatCon = new LookAtController(camera, null);
	view.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
	stage.stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
}
private function onMouseDown(e:MouseEvent):Void {
	
	active = true;
	lastPanAngle = hover.panAngle;
	lastTiltAngle = hover.tiltAngle;
	lastMouseX = stage.mouseX;
	lastMouseY = stage.mouseY;
	e.stopPropagation();
}
private function onMouseUp(e:MouseEvent):Void {
	active = false;
}
public function update():Void {
	if (active) {
	hover.panAngle = 0.4*(stage.mouseX - lastMouseX) + lastPanAngle;
	//hover.tiltAngle = 0.4*(stage.mouseY - lastMouseY) + lastTiltAngle;
	}
	hover.distance+=(lastDistance-hover.distance)*.1;
}
}